package com.kzd.gtgame.main

import android.Manifest
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Environment
import android.view.View
import com.blankj.utilcode.util.*
import com.kzd.gtgame.R
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.main.popup.ServiceCodePopup
import com.kzd.gtgame.utils.saveImageToGallery
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadRoundImage
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_service.*
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


class MainServiceFragment : OwnerViewModeFragment<BaseViewModel>() {

    private val mServiceCodePopup: ServiceCodePopup by lazy {
        ServiceCodePopup(context = requireContext()) { _, type ->
            val activity = this@MainServiceFragment.activity
                ?: return@ServiceCodePopup
            viewModel.userPermissions(activity, { isPermissions ->
                if (!isPermissions) {
                    toast("请到设置中开启相关权限")
                    return@userPermissions
                }
                val bitmap = BitmapFactory.decodeResource(resources,
                    if (type == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007)
                val fileName = "${if (type == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007}.png"
                if (saveImageToGallery(activity, bitmap, fileName)) {
                    toast("已保存")
                } else {
                    toast("保存失败")
                }
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
        }
    }

    companion object {
        fun newInstance(): MainServiceFragment {
            return MainServiceFragment()
        }
    }

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_service

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(flMainServiceBar)


        llMainServiceQQ.setOnClickAntiShakeListener {
            toExternalAppQQ(requireActivity())
        }

        llMainServiceWX.setOnClickAntiShakeListener {
//            val wx = "GT380000"
//            ClipboardUtils.copyText(wx)
//            toast("微信:$wx\t已复制，去微信搜索！")

            val image = viewModel.userServiceLiveData.value
                ?.data?.data?.qy_wx_img
            mServiceCodePopup.setContent(if (image.isNullOrEmpty()) {
                R.mipmap.ic_40008
            } else image, 1)
                .showPopupWindow()
        }

        ivMainServiceWX.setOnClickListener {
            val image = viewModel.userServiceLiveData.value
                ?.data?.data?.gzh_img
            mServiceCodePopup.setContent(if (image.isNullOrEmpty()) {
                R.mipmap.ic_40007
            } else image, 2)
                .showPopupWindow()
        }

        cbMainServicePlatformProblem.setOnCheckedChangeListener { _, isChecked ->
            tvMainServicePlatformContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        cbMainServiceGameProblem.setOnCheckedChangeListener { _, isChecked ->
            tvMainServiceGameContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        cbMainServiceTransactionProblem.setOnCheckedChangeListener { _, isChecked ->
            tvMainServiceTransactionContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        cbMainServiceCurrencyProblem.setOnCheckedChangeListener { _, isChecked ->
            tvMainServiceCurrencyContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        cbMainServiceMemberProblem.setOnCheckedChangeListener { _, isChecked ->
            tvMainServiceMemberContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        tvMainServicePlatformContent.text = StringBuilder()
            .append(getString(R.string.problem_details_platform_1_title)).append("\n")
            .append(getString(R.string.problem_details_platform_2_title)).append("\n")
            .append(getString(R.string.problem_details_platform_3_title)).append("\n")
            .append(getString(R.string.problem_details_platform_4_title))
        tvMainServicePlatformContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.PLATFORM)
        }

        tvMainServiceGameContent.text = StringBuilder()
            .append(getString(R.string.problem_details_game_1_title)).append("\n")
            .append(getString(R.string.problem_details_game_2_title)).append("\n")
            .append(getString(R.string.problem_details_game_3_title)).append("\n")
            .append(getString(R.string.problem_details_game_4_title)).append("\n")
            .append(getString(R.string.problem_details_game_5_title)).append("\n")
            .append(getString(R.string.problem_details_game_6_title))
        tvMainServiceGameContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.GAME)
        }

        tvMainServiceTransactionContent.text = StringBuilder()
            .append(getString(R.string.problem_details_transaction_1_title)).append("\n")
            .append(getString(R.string.problem_details_transaction_2_title)).append("\n")
            .append(getString(R.string.problem_details_transaction_3_title)).append("\n")
            .append(getString(R.string.problem_details_transaction_4_title))
        tvMainServiceTransactionContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.TRANSACTION)
        }

        tvMainServiceCurrencyContent.text = StringBuilder()
            .append(getString(R.string.problem_details_currency_1_title)).append("\n")
            .append(getString(R.string.problem_details_currency_2_title)).append("\n")
            .append(getString(R.string.problem_details_currency_3_title)).append("\n")
            .append(getString(R.string.problem_details_currency_4_title)).append("\n")
            .append(getString(R.string.problem_details_currency_5_title)).append("\n")
            .append(getString(R.string.problem_details_currency_6_title)).append("\n")
            .append(getString(R.string.problem_details_currency_7_title)).append("\n")
            .append(getString(R.string.problem_details_currency_8_title)).append("\n")
            .append(getString(R.string.problem_details_currency_9_title)).append("\n")
            .append(getString(R.string.problem_details_currency_10_title)).append("\n")
            .append(getString(R.string.problem_details_currency_11_title)).append("\n")
            .append(getString(R.string.problem_details_currency_12_title)).append("\n")
            .append(getString(R.string.problem_details_currency_13_title))
        tvMainServiceCurrencyContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.CURRENCY)
        }

        tvMainServiceMemberContent.text = StringBuilder()
            .append(getString(R.string.problem_details_member_1_title)).append("\n")
            .append(getString(R.string.problem_details_member_2_title)).append("\n")
            .append(getString(R.string.problem_details_member_3_title)).append("\n")
            .append(getString(R.string.problem_details_member_4_title)).append("\n")
            .append(getString(R.string.problem_details_member_5_title))
        tvMainServiceMemberContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.MEMBER)
        }


        ivMainServiceImage.setOnClickAntiShakeListener {
            val title = viewModel.userServiceLiveData.value
                ?.data?.data?.name
            val url = viewModel.userServiceLiveData.value
                ?.data?.data?.url
            val content = viewModel.userServiceLiveData.value
                ?.data?.data?.content
            if (!url.isNullOrEmpty()) {
                toMineRichTextActivityUrl(title, url)
            } else if (!content.isNullOrEmpty()) {
                toMineRichTextActivityCon(title, content)
            }
        }
    }

    override fun initData() {
        viewModel.userService()
    }

    override fun initViewMode() {
        viewModel.userServiceLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            ivMainServiceImage.loadRoundImage(src = data.image ?: R.mipmap.ic_40000,
                radius = ConvertUtils.dp2px(5f))
        })
    }
}